package com.zgjkhis.modules.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.zgjkhis.common.service.impl.BaseServiceImpl;
import com.zgjkhis.domain.PageResult;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.modules.enums.DrugTypeEnum;
import com.zgjkhis.modules.enums.WareHousingTypeEnum;
import com.zgjkhis.modules.enums.WarehousingStatusEnum;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.system.domain.Drug;
import com.zgjkhis.modules.system.domain.DrugWareHousing;
import com.zgjkhis.modules.system.domain.DrugWareHousingDetail;
import com.zgjkhis.modules.system.domain.vo.DrugWareHousingDetailVO;
import com.zgjkhis.modules.system.domain.vo.DrugWareHousingVo;
import com.zgjkhis.modules.system.service.DrugService;
import com.zgjkhis.modules.system.service.DrugWareHousingDetailService;
import com.zgjkhis.modules.system.service.DrugWareHousingService;
import com.zgjkhis.modules.system.service.dto.DrugQueryCriteria;
import com.zgjkhis.modules.system.service.dto.DrugWareHousingQueryCriteria;
import com.zgjkhis.modules.system.service.mapper.DrugWareHousingMapper;
import com.zgjkhis.modules.util.CharUtil;
import com.zgjkhis.utils.DateUtils;
import com.zgjkhis.utils.FileUtil;
import com.zgjkhis.utils.RandomUtils;
import com.zgjkhis.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @Author yzq
 * @Description 药品入库
 * @Date 2021/6/23 9:47
 * @Param
 * @return
 **/
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DrugWareHousingServiceImpl extends BaseServiceImpl<DrugWareHousingMapper, DrugWareHousing> implements DrugWareHousingService {

    private final DrugService drugService;
    private final DrugWareHousingDetailService drugWareHousingDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createDrugWareHousing(DrugWareHousingVo drugWareHousing) {
        DrugWareHousing wareHousing = new DrugWareHousing();
        String wareHousingNo = DateUtils.dateTimeNow() + RandomUtils.getRandomNumbers(6);
        wareHousing.setId(drugWareHousing.getId());
        if (!CharUtil.isEmpty(drugWareHousing.getId())) {
            DrugWareHousing temp = baseMapper.selectById(drugWareHousing.getId());
            if (CharUtil.isEmpty(temp) || temp.getStatus().equals(WarehousingStatusEnum.YRK.getCode())) {
                throw new ServiceException("入库单子【" + drugWareHousing.getId() + "】不存在,或状态不对");
            }
            wareHousingNo = temp.getWareHousingNo();
        }
        wareHousing.setWareHousingNo(wareHousingNo);
        wareHousing.setWareHousingType(drugWareHousing.getWareHousingType());
        wareHousing.setStatus(drugWareHousing.getStatus());
        wareHousing.setSupplierId(drugWareHousing.getSupplierId());
        wareHousing.setSupplierName(drugWareHousing.getSupplierName());
        wareHousing.setDrugType(drugWareHousing.getDrugType());
        wareHousing.setPayStatus(drugWareHousing.getPayStatus());
        wareHousing.setRefundContent(drugWareHousing.getRefundContent());
        wareHousing.setWarehousingQuantity(drugWareHousing.getWarehousingQuantity());


        if (drugWareHousing.getStatus().equals(WarehousingStatusEnum.YISH.getCode()) || drugWareHousing.getStatus().equals(WarehousingStatusEnum.YBH.getCode())) {
            wareHousing.setCheckUserId(SecurityUtils.getUserId().toString());
            wareHousing.setCheckUserName(LoginUserUtil.getNickName());
            wareHousing.setCheckTime(new Date());
        } else {
            //审核驳回不该创建人
            wareHousing.setCreateUserId(SecurityUtils.getUserId().toString());
            wareHousing.setCreateUserName(LoginUserUtil.getNickName());
        }
        if (drugWareHousing.getStatus().equals(WarehousingStatusEnum.YRK.getCode())) {
            wareHousing.setWarehousingUserId(SecurityUtils.getUserId().toString());
            wareHousing.setWarehousingUserName(LoginUserUtil.getNickName());
            wareHousing.setWarehousingTime(new Date());
        }
        wareHousing.setRemark(drugWareHousing.getRemark());
        wareHousing.setPharmacyId(drugWareHousing.getPharmacyId());
        BigDecimal total = BigDecimal.ZERO;
        List<DrugWareHousingDetail> list = new ArrayList<>();
        if (CharUtil.isEmpty(drugWareHousing.getList())) {
            throw new ServiceException("入库清单没有药品");
        }
        //清空原来的，新入库
        drugWareHousingDetailService.remove(Wrappers.<DrugWareHousingDetail>query().lambda().eq(DrugWareHousingDetail::getWareHousingNo, wareHousingNo));
        for (DrugWareHousingDetail drugWareHousingDetail : drugWareHousing.getList()) {
            if (CharUtil.isEmpty(drugWareHousingDetail.getDrugId())) {
                throw new ServiceException("请完善药品清单再入库(存在药品id为空的药品)");
            }
            DrugWareHousingDetail detail = new DrugWareHousingDetail();
            Drug drug = drugService.getOne(Wrappers.<Drug>query().lambda().eq(Drug::getId, drugWareHousingDetail.getDrugId()));
            if (CharUtil.isEmpty(drug)) {
                throw new ServiceException("药品Id(" + drugWareHousingDetail.getDrugId() + ")不存在");
            }
            if (drug.getDrugType().equals(DrugTypeEnum.PFKL.getCode()) && CharUtil.isEmpty(drugWareHousingDetail.getEquivalent())) {
                throw new ServiceException("配方颗粒当量不能为空");
            }
//            if(drug.getDrugType().equals(DrugTypeEnum.PFKL.getCode())){
//                Drug tempDrug=new Drug();
//                tempDrug.setId(drug.getId());
//                tempDrug.setDrugEquivalent(drugWareHousingDetail.getEquivalent());
//                tempDrug.setCoefficient(drugWareHousingDetail.getCoefficient());
//                drugService.updateById(tempDrug);
//            }
            detail.setDrugId(drugWareHousingDetail.getDrugId());
            detail.setDrugName(drug.getDrugName());
            if (CharUtil.isEmpty(drugWareHousingDetail.getScheduledQuantity())) {
                throw new ServiceException("药品：【" + drug.getDrugName() + "】预定数量不能为空");
            }
            detail.setScheduledQuantity(drugWareHousingDetail.getScheduledQuantity());
            if (!CharUtil.isEmpty(drugWareHousingDetail.getActualQuantity())) {
                detail.setLeftCount((int) (drugWareHousingDetail.getActualQuantity() * drugWareHousingDetail.getCoefficient()));

            }
            detail.setStock(drug.getStock());
            detail.setMinStock(drug.getMinStock());
            detail.setSalePrice(drug.getSalePrice());
            if (!drugWareHousing.getWareHousingType().equals(WareHousingTypeEnum.TBRK)) {
                if (CharUtil.isEmpty(drugWareHousingDetail.getCostPrice())) {
                    throw new ServiceException("本次成本价不能为空");
                }
            }
            detail.setCostPrice(drugWareHousingDetail.getCostPrice());
            detail.setTermOfValidity(drugWareHousingDetail.getTermOfValidity());
            if (CharUtil.isEmpty(drugWareHousingDetail.getBatchNo())
                    && drugWareHousing.getStatus().equals(WarehousingStatusEnum.YRK.getCode())) {
                throw new ServiceException("药品：【" + drug.getDrugName() + "】批次号不能为空");
            }
            detail.setBatchNo(drugWareHousingDetail.getBatchNo());
            detail.setId(drugWareHousingDetail.getId());
            detail.setWareHousingNo(wareHousingNo);
            detail.setDrugSpec(drug.getDrugSpec());
            detail.setDrugManufactor(drug.getDrugManufactor());
            detail.setPackageUnit(drug.getPackageUnit());
            //药材为3中成药，4西药，5耗材 存在零售,总剂量剂量单位
            if (DrugTypeEnum.ZCY.getCode().equals(drug.getDrugType()) ||
                    DrugTypeEnum.XY.getCode().equals(drug.getDrugType()) || DrugTypeEnum.OTHER.getCode().equals(drug.getDrugType()) ||
                    DrugTypeEnum.HC.getCode().equals(drug.getDrugType())) {
                //总剂量单位
                detail.setDoseUnit(drug.getTotalDoseUnit());
            } else {
                //剂量单位
                detail.setDoseUnit(drug.getDoseUnit());
            }
            detail.setPharmacyId(drugWareHousing.getPharmacyId());
            detail.setSelfPrice(drugWareHousingDetail.getSelfPrice());
            detail.setRedPun(drugWareHousingDetail.getRedPun());
            detail.setEquivalent(drugWareHousingDetail.getEquivalent());
            detail.setCoefficient(drugWareHousingDetail.getCoefficient());
            detail.setCoefficientName(drugWareHousingDetail.getCoefficientName());
            detail.setActualQuantity(drugWareHousingDetail.getActualQuantity());
            detail.setCommodityPrice(drugWareHousingDetail.getCommodityPrice());
            detail.setCommoditySelfPrice(drugWareHousingDetail.getCommoditySelfPrice());
            detail.setWarehousingUnit(drugWareHousingDetail.getWarehousingUnit());
            if (CharUtil.isEmpty(drugWareHousingDetail.getWarehousingUnit())) {
                detail.setWarehousingUnit(drugWareHousingDetail.getCoefficientName());
            }
            if (drugWareHousing.getStatus().equals(WarehousingStatusEnum.YRK.getCode())) {
                if (CharUtil.isEmpty(drugWareHousingDetail.getActualQuantity())) {
                    drugWareHousingDetail.setActualQuantity(drugWareHousingDetail.getScheduledQuantity());
                }
                //入库修改药品数量
                Drug tempDrug = new Drug();
                tempDrug.setId(drug.getId());
                //颗粒剂库存=入库*换算系数
//                if(DrugTypeEnum.PFKL.getCode().equals(drug.getDrugType())){
//                    tempDrug.setStock(CharUtil.isEmpty(drug.getStock())?0:drug.getStock()+(drugWareHousingDetail.getActualQuantity()*drugWareHousingDetail.getCoefficient()));
//                }else{
//                    tempDrug.setStock(CharUtil.isEmpty(drug.getStock())?0:drug.getStock()+drugWareHousingDetail.getActualQuantity());
//                }

                if (CharUtil.isEmpty(drugWareHousingDetail.getCoefficient())) {
                    tempDrug.setStock(CharUtil.isEmpty(drug.getStock()) ? 0 : drug.getStock() + drugWareHousingDetail.getActualQuantity());
                } else {
                    tempDrug.setStock((int) (CharUtil.isEmpty(drug.getStock()) ? 0 : drug.getStock() + (drugWareHousingDetail.getActualQuantity() * drugWareHousingDetail.getCoefficient())));
                }

                drugService.updateById(tempDrug);
            }

            if (CharUtil.isEmpty(drugWareHousingDetail.getActualQuantity())) {
                total = total.add(drugWareHousingDetail.getCostPrice().multiply(BigDecimal.valueOf(drugWareHousingDetail.getCoefficient())).multiply(new BigDecimal(drugWareHousingDetail.getScheduledQuantity())));
            } else {
                total = total.add(drugWareHousingDetail.getCostPrice().multiply(BigDecimal.valueOf(drugWareHousingDetail.getCoefficient())).multiply(new BigDecimal(drugWareHousingDetail.getActualQuantity())));
            }
            list.add(detail);
        }
        //总金额
        wareHousing.setOrderTotal(total);
        this.saveOrUpdate(wareHousing);
        drugWareHousingDetailService.saveOrUpdateBatch(list);
        return true;
    }

    @Override
    public PageResult<DrugWareHousing> findAllByPageable(DrugWareHousingQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<DrugWareHousing> page = new PageInfo<>(baseMapper.findAllByPageable(criteria));
        return PageResult.data(page);
    }

    @Override
    public void findAllByPageableForOut(DrugWareHousingQueryCriteria criteria, HttpServletResponse response) throws IOException {
        List<DrugWareHousing> allList = baseMapper.findAllByPageable(criteria);
        List<Map<String, Object>> list = new ArrayList<>();
        for (DrugWareHousing drugWareHousing : allList) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("订单号", drugWareHousing.getWareHousingNo());
            map.put("订单日期", drugWareHousing.getCreateTime());
            map.put("入库方式", WareHousingTypeEnum.findByCode(drugWareHousing.getWareHousingType()));
            map.put("订单金额", drugWareHousing.getOrderTotal());
            map.put("制单人", drugWareHousing.getCreateUserName());
            map.put("订单状态", WarehousingStatusEnum.findByCode(drugWareHousing.getStatus()));
            map.put("审核人", drugWareHousing.getCheckUserName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


    @Override
    public void outTemplate(DrugQueryCriteria criteria, HttpServletResponse response) throws IOException {
        //    List<Drug> allList = drugMapper.selectList(QueryHelpPlus.getPredicate(Drug.class, criteria));
        // List<Drug> allList = drugService.findAllByPageable(QueryHelpPlus.getPredicate(Drug.class, criteria));
        List<Drug> allList = drugService.findAllByPageable(criteria);

        List<Map<String, Object>> list = new ArrayList<>();
        for (Drug drug : allList) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("商品id", drug.getId());
            map.put("商品名称", drug.getDrugName());
            map.put("采购单位 例:kg", "");
            if (drug.getDrugType() == 2) {
                map.put("换算系数 例:1000", drug.getCoefficient());
                map.put("克当量 例:10", drug.getDrugEquivalent());
            } else {
                map.put("换算系数 例:1000", "");
                map.put("克当量 例:10", "");
            }
            map.put("进货价", "");
            map.put("预定数量", "");
            map.put("批次号", "");
            map.put("有效期 例:2022-01-01", "");
            map.put("(颗粒剂必填)红冲% 例:80", "");
            map.put("药品类型", DrugTypeEnum.findByCode(drug.getDrugType()));
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


    @Override
    public void inTemplate(MultipartFile file, Integer status, Integer wareHousingType, Long supplierId, String supplierName, Integer drugType) throws IOException, ParseException {
        ExcelReader excelReader = ExcelUtil.getReader(file.getInputStream());
        DrugWareHousing wareHousing = new DrugWareHousing();
        String wareHousingNo = DateUtils.dateTimeNow() + RandomUtils.getRandomNumbers(6);
        wareHousing.setWareHousingNo(wareHousingNo);
        wareHousing.setWareHousingType(wareHousingType);
        if (wareHousingType == 1) {
            wareHousing.setStatus(1);
        } else {
            wareHousing.setStatus(7);
        }
        BigDecimal total = BigDecimal.ZERO;
        wareHousing.setSupplierId(supplierId);
        wareHousing.setSupplierName(supplierName);
        wareHousing.setPharmacyId(LoginUserUtil.getPharmId());
        wareHousing.setCreateUserId(SecurityUtils.getUserId().toString());
        wareHousing.setCreateUserName(LoginUserUtil.getNickName());
        wareHousing.setDrugType(drugType);
        List<List<Object>> listAll = excelReader.read();
        List<DrugWareHousingDetail> list = new ArrayList<>();
        for (int i = 1; i < listAll.size(); i++) {
            DrugWareHousingDetail temp = new DrugWareHousingDetail();
            List<Object> rowlist = listAll.get(i);
            temp = saveByFlag1(rowlist, wareHousing);
            if (!CharUtil.isEmpty(temp)) {
                list.add(temp);
                total = total.add(temp.getCostPrice().multiply(new BigDecimal(temp.getScheduledQuantity())));
            }
        }
        wareHousing.setOrderTotal(total);
        wareHousing.setWarehousingQuantity(list.size());
        drugWareHousingDetailService.saveBatch(list);
        this.saveOrUpdate(wareHousing);

    }


    private DrugWareHousingDetail saveByFlag1(List<Object> rowlist, DrugWareHousing wareHousing) throws ParseException {

        DrugWareHousingDetail temp = new DrugWareHousingDetail();
        String drugId = CharUtil.isEmpty(rowlist.get(0)) ? "" : rowlist.get(0).toString();
        String drugName = CharUtil.isEmpty(rowlist.get(1)) ? "" : rowlist.get(1).toString();
        String coefficientName = CharUtil.isEmpty(rowlist.get(2)) ? "" : rowlist.get(2).toString();
        String coefficient = CharUtil.isEmpty(rowlist.get(3)) ? "" : rowlist.get(3).toString();
        String drugEquivalent = CharUtil.isEmpty(rowlist.get(4)) ? "" : rowlist.get(4).toString();
        String costPrice = CharUtil.isEmpty(rowlist.get(5)) ? "" : rowlist.get(5).toString();
        String scheduledQuantity = CharUtil.isEmpty(rowlist.get(6)) ? "" : rowlist.get(6).toString();
        String batchNo = CharUtil.isEmpty(rowlist.get(7)) ? "" : rowlist.get(7).toString();
        String termOfValidity = CharUtil.isEmpty(rowlist.get(8)) ? "" : rowlist.get(8).toString();
        String redPun = CharUtil.isEmpty(rowlist.get(9)) ? "0" : rowlist.get(9).toString();
        Drug drug = drugService.getOne(Wrappers.<Drug>query().lambda().eq(Drug::getId, drugId));


        if (!CharUtil.isEmpty(costPrice)) {
            if (CharUtil.isEmpty(coefficientName) || CharUtil.isEmpty(coefficient) || CharUtil.isEmpty(scheduledQuantity) ||
                    CharUtil.isEmpty(batchNo)) {
                throw new ServiceException("信息不全导入失败");
            }
            if (wareHousing.getDrugType() == 1) {
                if (drug.getDrugType() != 1) {
                    throw new ServiceException("药品：【 " + drug.getDrugName() + "】类型与选择的不一致");
                }
            } else if (wareHousing.getDrugType() == 2) {
                if (drug.getDrugType() != 2) {
                    throw new ServiceException("药品：【 " + drug.getDrugName() + "】类型与选择的不一致");
                }
            } else if (wareHousing.getDrugType() == 7) {
                if (drug.getDrugType() != 3 && drug.getDrugType() != 4) {
                    throw new ServiceException("药品：【 " + drug.getDrugName() + "】类型与选择的不一致");
                }
            }

            temp.setDrugId(Long.valueOf(drugId));
            temp.setDrugName(drug.getDrugName());
            if (CharUtil.isEmpty(scheduledQuantity)) {
                throw new ServiceException("药品：【" + drug.getDrugName() + "】预定数量不能为空");
            }
            temp.setScheduledQuantity(Integer.valueOf(scheduledQuantity));
//            if(wareHousing.getWareHousingType()!=1){
            temp.setActualQuantity(Integer.valueOf(scheduledQuantity));
//            }

            BigDecimal quantity = new BigDecimal(Integer.valueOf(scheduledQuantity));

            temp.setLeftCount((int) (Integer.valueOf(scheduledQuantity) * Double.valueOf(coefficient)));

            temp.setCommodityPrice(quantity.multiply(BigDecimal.valueOf(Double.valueOf(costPrice))));
            temp.setStock(drug.getStock());
            temp.setMinStock(drug.getMinStock());
            temp.setSalePrice(drug.getSalePrice());
            temp.setCostPrice(BigDecimal.valueOf(Double.valueOf(costPrice)));
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            if (!CharUtil.isEmpty(termOfValidity)) {
                temp.setTermOfValidity(format.parse(termOfValidity));
            }
            temp.setBatchNo(batchNo);
            temp.setWareHousingNo(wareHousing.getWareHousingNo());
            temp.setDrugSpec(drug.getDrugSpec());
            temp.setDrugManufactor(drug.getDrugManufactor());
            temp.setPackageUnit(drug.getPackageUnit());
            //药材为3中成药，4西药，5耗材 存在零售,总剂量剂量单位
            if (DrugTypeEnum.ZCY.getCode().equals(drug.getDrugType()) ||
                    DrugTypeEnum.XY.getCode().equals(drug.getDrugType()) || DrugTypeEnum.OTHER.getCode().equals(drug.getDrugType()) ||
                    DrugTypeEnum.HC.getCode().equals(drug.getDrugType())) {
                //总剂量单位
                temp.setDoseUnit(drug.getTotalDoseUnit());
            } else {
                //剂量单位
                temp.setDoseUnit(drug.getDoseUnit());
            }
            temp.setPharmacyId(wareHousing.getPharmacyId());
            if (!CharUtil.isEmpty(redPun)) {
                if (Integer.valueOf(redPun) > 100 || Integer.valueOf(redPun) < 0) {
                    throw new ServiceException("药品：【" + drug.getDrugName() + "】红冲字段错误");
                } else {
                    temp.setRedPun((BigDecimal.valueOf(Double.valueOf(redPun))));
                    temp.setSelfPrice((new BigDecimal(100).subtract((BigDecimal.valueOf(Double.valueOf(redPun))))).divide(new BigDecimal(100)).multiply(BigDecimal.valueOf(Double.valueOf(costPrice))));
                    temp.setCommoditySelfPrice((new BigDecimal(100).subtract(((BigDecimal.valueOf(Double.valueOf(redPun))))).multiply(quantity)).divide(new BigDecimal(100)).multiply(BigDecimal.valueOf(Double.valueOf(costPrice))));
                }
            }
            temp.setCoefficient(CharUtil.isEmpty(coefficient) ? null : Double.valueOf(coefficient));
            temp.setEquivalent(CharUtil.isEmpty(drugEquivalent) ? null : Double.valueOf(drugEquivalent));
            temp.setCoefficientName(coefficientName);
            return temp;
        } else {
            return null;
        }
    }

    @Override
    public void updatePayStatus(Long id, int payStatus) {
        DrugWareHousing temp = this.baseMapper.selectById(id);
        if (CharUtil.isEmpty(temp)) {
            throw new ServiceException("入库单子【" + id + "】不存在,或状态不对");
        }
        temp.setPayStatus(payStatus);
        this.baseMapper.updateById(temp);
    }


    @Override
    public void outWareHousing(String id, HttpServletResponse response) throws IOException {
        //    List<Drug> allList = drugMapper.selectList(QueryHelpPlus.getPredicate(Drug.class, criteria));
        // List<Drug> allList = drugService.findAllByPageable(QueryHelpPlus.getPredicate(Drug.class, criteria));
        DrugWareHousing drugWareHousing = this.getById(id);
        DrugWareHousingVo drugWareHousingVo = new DrugWareHousingVo();
        BeanUtil.copyProperties(drugWareHousing, drugWareHousingVo);
        List<DrugWareHousingDetailVO> detailVOS = drugWareHousingDetailService.queryList(drugWareHousing.getWareHousingNo());
        drugWareHousingVo.setList(detailVOS);

        List<Map<String, Object>> list = new ArrayList<>();
        for (DrugWareHousingDetailVO detailVO : detailVOS) {
            Map<String, Object> map = new LinkedHashMap<>();
            String type = "";
            switch (Integer.valueOf(detailVO.getDrugType())) {
                case 1:
                    type = "中药饮片";
                    break;
                case 2:
                    type = "配方颗粒";
                    map.put("红冲", detailVO.getRedPun());
                    map.put("成本价", detailVO.getSelfPrice());
                    break;
                case 3:
                    type = "中成药";
                    break;
                case 4:
                    type = "西药";
                    break;
                case 5:
                    type = "耗材";
                    break;
                case 6:
                    type = "敷贴粉剂";
                    break;
            }
            map.put("商品名称", detailVO.getDrugName());
            map.put("采购单位", detailVO.getCoefficientName());
            map.put("换算系数", detailVO.getCoefficient());
            map.put("进货价", detailVO.getCostPrice());
            map.put("预定数量", detailVO.getScheduledQuantity());
            map.put("实际数量", detailVO.getActualQuantity());
            map.put("批次号", detailVO.getBatchNo());
            map.put("有效期", detailVO.getTermOfValidity());
            map.put("药品类型", type);
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void outDetail(DrugWareHousingQueryCriteria criteria, HttpServletResponse response) throws IOException {
        List<DrugWareHousingDetailVO> details = baseMapper.findDetailBy(criteria);
        List<Map<String, Object>> list = new ArrayList<>();
        for (DrugWareHousingDetailVO detailVO : details) {
            Map<String, Object> map = new LinkedHashMap<>();
            String type = "";
            switch (Integer.valueOf(detailVO.getDrugType())) {
                case 1:
                    type = "中药饮片";
                    break;
                case 2:
                    type = "配方颗粒";
                    map.put("红冲", detailVO.getRedPun());
                    map.put("成本价", detailVO.getSelfPrice());
                    break;
                case 3:
                    type = "中成药";
                    break;
                case 4:
                    type = "西药";
                    break;
                case 5:
                    type = "耗材";
                    break;
                case 6:
                    type = "敷贴粉剂";
                    break;
            }
            map.put("药品ID", detailVO.getDrugId());
            map.put("药品名称", detailVO.getDrugName());
            map.put("规格", detailVO.getDrugSpec());
            map.put("采购单位", detailVO.getCoefficientName());
            map.put("换算系数", detailVO.getCoefficient());
            map.put("进货价", detailVO.getSelfPrice());
            map.put("预定数量", detailVO.getScheduledQuantity());
            map.put("实际数量", detailVO.getActualQuantity());
            map.put("金额", detailVO.getCommoditySelfPrice());
            map.put("入库日期", detailVO.getCreateTime());
            map.put("供应商", detailVO.getSupplierName());
            map.put("批次号", detailVO.getBatchNo());
            map.put("有效期", detailVO.getTermOfValidity());
            map.put("药品类型", type);
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
